package cn.jsony.product.service.impl;

import cn.jsony.basic.service.IBaiduAiAuditService;
import cn.jsony.basic.util.AjaxResult;
import cn.jsony.basic.util.LoginContext;
import cn.jsony.org.domain.Employee;
import cn.jsony.org.mapper.EmployeeMapper;
import cn.jsony.pet.domain.Pet;
import cn.jsony.pet.domain.PetOnlineAuditLog;
import cn.jsony.product.domain.Product;
import cn.jsony.product.domain.ProductDetail;
import cn.jsony.product.domain.ProductOnlineAuditLog;
import cn.jsony.product.mapper.ProductDetailMapper;
import cn.jsony.product.mapper.ProductMapper;
import cn.jsony.product.mapper.ProductOnlineAuditLogMapper;
import cn.jsony.product.service.IProductService;
import cn.jsony.basic.service.impl.BaseServiceImpl;
import cn.jsony.user.domain.Logininfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Autowired
    private EmployeeMapper employeeMapper;


    @Override
    public void add(Product product) {
        super.add(product);//自增id
        if (product.getProductDetail() != null) {
            ProductDetail productDetail = product.getProductDetail();
            productDetail.setProductId(product.getId());
            productDetailMapper.save(productDetail);
        }
    }

    @Override
    public void del(Long id) {
        super.del(id);
        //根据产品的id删除产品详情
        productDetailMapper.removeByProductId(id);
    }

    @Override
    public void update(Product product) {
        super.update(product);
        if (product.getProductDetail() != null) {
            ProductDetail productDetail = product.getProductDetail();
            productDetail.setProductId(product.getId());
            //产品详情更新：根据产品详情id去更新
            productDetailMapper.update(productDetail);
        }
    }

    @Override
    public AjaxResult onsale(List<Long> ids, HttpServletRequest request) {
        //上架 -不能用批量操作，有上架审核
        for (Long id : ids) {
            //上架自动审核文本
            Product product = productMapper.loadById(id);
            String auditText = product.getName();
            Boolean textBoo = baiduAiAuditService.textAudit(auditText);
            //审核图片：多张图片resources
            String productResources = product.getResources();
            Boolean imageBoo = true;
            if(!StringUtils.isEmpty(productResources)){
                imageBoo = baiduAiAuditService.imageAudit(productResources);
            }

            //获取审核人
            Logininfo logininfo = (Logininfo) LoginContext.getLogininfo(request);
            Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());

            //审核通过
            if (textBoo && imageBoo){
                //数据库操作
                Map<String,Object> params = new HashMap<>();
                params.put("id",id);
                params.put("onsaletime",new Date());
                productMapper.onsale(params);
                //审核成功-note

                //记录审核日志
                ProductOnlineAuditLog auditLog = new ProductOnlineAuditLog();
                auditLog.setState(1);
                auditLog.setProductId(id);
                auditLog.setAuditId(employee.getId());
                auditLog.setNote("审核成功！");
                productOnlineAuditLogMapper.save(auditLog);
            }else{
                //审核失败-note-当前是下架状态，如果审核失败！
                //记录审核日志
                ProductOnlineAuditLog auditLog = new ProductOnlineAuditLog();
                auditLog.setState(0);
                auditLog.setProductId(id);
                auditLog.setAuditId(employee.getId());
                auditLog.setNote("审核失败,宠物名称或图片不合法!!!");
                productOnlineAuditLogMapper.save(auditLog);
                //审核失败，上架失败
                return AjaxResult.me().setSuccess(false).setMsg("上架失败！审核失败,宠物名称或图片不合法!!!");
            }
        }
        return AjaxResult.me();
    }

    @Override
    public AjaxResult offsale(List<Long> ids, HttpServletRequest request) {
        //下架：修改state为0，offsaletime为当前时间 ,可以用批量操作

        Map<String,Object> params = new HashMap<>();
        params.put("ids",ids);
        params.put("offsaletime",new Date());
        productMapper.offsale(params);
        return AjaxResult.me();
    }
}

